//
//  UserViewController.m
//  JavaBaas_iOS_SDK
//
//  Created by zhaopeng on 16/1/27.
//  Copyright © 2016年 Buge. All rights reserved.
//

#import "UserViewController.h"
#import "JavaBaas.h"

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

//登录
- (IBAction)loginClick:(UIButton *)sender {
    /**
     *  用户登录: 目前用户登录可以通过用户名密码／第三方登录两种方式，其中第三方登录目前支持QQ／微信／微博
     *  第三方登录：需要一个authData 
     *  authData: 有2个参数 1.accessToken 2.uid(微博)或 unionId和openId二选一(微信和qq)
     *  其中accessToken 和 uid、unionId/openId是从第三方获取
     */
    
#if 1
//   1 用户名密码登录(同步)
    NSError *error = nil;
    id responseObject = [JBUser loginWithUsername:@"iosUserName" password:@"javabaas" error:&error];
    if (!error) {
        NSLog(@"登录成功:%@", responseObject);
    } else {
        NSLog(@"登录失败：%@", error);
    }
//    [JBUser loginWithUsernameInBackground:@"iosUserName" password:@"javabaas" block:^(JBUser *user, NSError *error) {
//        //   2 异步登录
//        if (!error) {
//            NSLog(@"登录成功:%@", [user dictionaryForObject]);
//        }else {
//            NSLog(@"%@", error);
//        }
//    }];
#endif
    

#if 0
//    第三方登录
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:@"2.006INLBG43cyQD40b1048e0edKRhTC" forKey:@"accessToken"];
    [dictionary setObject:@"5514245429" forKey:@"uid"];
    
    [JBUser loginWithAuthDataInBackground:dictionary authType:JBPlatformSinaWeibo block:^(JBUser *user, NSError *error) {
        
        if (!error) {
            NSLog(@"登录成功:%@", user);
        }
    }];
    
    
#endif
    
    
}



//绑定
- (IBAction)bingClick:(UIButton *)sender {
    /**
     *  绑定：需要第三方授权信息以及第三方平台（通用有同步异步方法）
     */
    
    JBUser *user = [JBUser objectWithoutDataWithObjectId:@"91cba7e7545f4c46ac3c5d8ba4949d6a"];
    NSMutableDictionary *authData = [NSMutableDictionary dictionary];
    [authData setObject:@"2.006INLBG43cyQD40b1048e0edKRhTC" forKey:@"accessToken"];
    [authData setObject:@"5514245429" forKey:@"uid"];
    
    [user addAuthDataInBackground:authData platform:JBPlatformSinaWeibo block:^(id object, NSError *error) {
        if (!error) {
            NSLog(@"绑定成功: %@", object);
        }else {
            NSLog(@"%@", error);
        }
    }];
}

//解绑
- (IBAction)deleAuthClick:(UIButton *)sender {
    JBUser *user = [JBUser objectWithoutDataWithObjectId:@"8b6eca5c35844fdea560de0efec14a7e"];
    [user deleteAuthDataForPlatformInBackground:JBPlatformSinaWeibo block:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSLog(@"解绑成功");
        }else {
            NSLog(@"%@", error);
        }
    }];
}

//修改信息
- (IBAction)updateInfoClick:(UIButton *)sender {
    JBUser *user = [JBUser objectWithoutDataWithObjectId:@"7efd7571ac6446c1bc3bd60a490be8eb"];
    
    [user setObject:[NSNumber numberWithInteger:3] forKey:@"age"];
    user.email = @"qq@163.com";
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSLog(@"修改成功");
        } else {
            NSLog(@"修改错误:%@", error);
        }
    }];
}

- (IBAction)updatePasswordClick:(UIButton *)sender {
    
    JBUser *user = [JBUser objectWithoutDataWithObjectId:@"7efd7571ac6446c1bc3bd60a490be8eb"];
    
    [user updatePassword:@"javabaas" newPassword:@"javabaas" block:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"%@", error);
        } else {
            NSLog(@"修改成功");
        }
    }];
}

@end













